package service

import (
	"egin/app/models"
	"egin/app/request"
	"egin/app/responses"
	"egin/pkg/database"
	"egin/pkg/utils"
	"errors"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

func XmSelectList() ([]responses.XmSelectList, error) {
	var list []responses.XmSelectList
	err := database.Db().Model(&models.Role{}).Select(models.RoleColumns.ID, models.RoleColumns.Name).Find(&list).Error
	if err != nil {
		zap.L().Error("GetRoleList", zap.Error(err))
		return nil, err
	}

	return list, err
}

/**
*GetRoleList 获取角色列表数据
*@return database.Paginate 数据（分页）
*@return error 错误
 */
func GetRoleList(c *gin.Context) (*database.Paginate, error) {
	roleobj := database.Db()

	if name := c.PostForm("name"); name != "" {
		roleobj = roleobj.Where("name = ?", name)
	}

	var list []models.Role
	p := &database.Paginate{}

	err := roleobj.Order("id desc").Scopes(p.Paginates(c)).Find(&list).Error

	if err != nil {
		zap.L().Error("GetRoleList", zap.Error(err))
		return nil, err
	}
	p.List = list
	return p, err
}

func GetRole(c *gin.Context) (*models.Role, error) {
	id := c.Param("id")

	var list *models.Role

	err := database.Db().Preload("Permission", func(db *gorm.DB) *gorm.DB {
		return db.Select("id,title")
	}).Where("id = ?", id).Find(&list).Error
	if err != nil {
		zap.L().Error("GetRoleList", zap.Error(err))
		return nil, err
	}

	return list, err
}

/**
*RoleAdd 添加角色
*@param param  request.RoleQs 绑定的请亲数据
*@return error
 */
func RoleAdd(param request.RoleQs, c *gin.Context) error {

	c.Request.ParseForm()
	c.Request.PostForm.Del("name")
	c.Request.PostForm.Del("describe")
	c.Request.PostForm.Del("layuiTreeCheck_0")

	var permissionList []models.Permission

	for _, v := range c.Request.PostForm {

		permissionList = append(permissionList, models.Permission{ID: utils.StrToUInt(v[0])})

	}

	role := models.Role{
		Name:       param.Name,
		Describe:   param.Describe,
		Permission: permissionList,
	}

	result := database.Db().Create(&role)
	return result.Error
}

/**
*RoleEdit 修改角色
*@param param  request.RoleQs 绑定的请亲数据
*@return error
 */
func RoleEdit(param request.RoleEditQs, c *gin.Context) error {

	c.Request.ParseForm()
	c.Request.PostForm.Del("name")
	c.Request.PostForm.Del("describe")
	c.Request.PostForm.Del("layuiTreeCheck_0")
	c.Request.PostForm.Del("id")

	var permissionList []models.Permission

	for _, v := range c.Request.PostForm {

		permissionList = append(permissionList, models.Permission{ID: utils.StrToUInt(v[0])})

	}

	var role models.Role
	database.Db().Where("id = ?", param.Id).First(&role)
	database.Db().Model(&role).Association("Permission").Clear()

	role.Name = param.Name
	role.Describe = param.Describe
	role.Permission = permissionList
	result := database.Db().Save(&role)
	return result.Error
}

/**
*RoleDelete 删除角色
*@return error
 */
func RoleDelete(c *gin.Context) error {
	id := c.Param("id")

	var role models.Role
	database.Db().Where("id = ?", id).Preload("Member").First(&role)
	if len(role.Member) > 0 {
		return errors.New("有管理员是改角色")
	}

	database.Db().Model(&role).Association("Permission").Clear()
	err := database.Db().Where("id = ?", id).Delete(&models.Role{}).Error
	if err != nil {
		return errors.New("删除失败")
	}
	return nil

}
